//
//  SINAAskForLeaveViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/21.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAAskForLeaveViewController.h"
#import "SINAStudentAskLeaveTypeSelectView.h"
#import "SINACommonTimePickView.h"
#import "SINAPickTimeView.h"
#import "SINAPickCityView.h"
#import "SINACustomPickView.h"
#import "SINACustomModelPickView.h"
#import "SINASchoolModel.h"
#import "SINAAskListPageViewController.h"
#import "SINAPhotoPopView.h"
#import "SINACalendarUtils.h"

@interface SINAAskForLeaveViewController () <UITextViewDelegate,CommonTimeSelectViewDelegate,PickerCityViewDelegate,PickTimeViewDelegate,CustomPickViewDelegate,CustomModelPickViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollview;

//请假类型
@property(nonatomic,strong) UILabel *askLabel;
@property(nonatomic,strong) SINASchoolModel *askTypeModel;
//请假开始时间
@property(nonatomic,strong) UILabel *askTimeStartLabel;
@property(nonatomic,assign) NSInteger startYear;
@property(nonatomic,assign) NSInteger startMonth;
@property(nonatomic,assign) NSInteger startDay;
@property(nonatomic,assign) NSInteger startHour;
@property(nonatomic,assign) NSInteger endYear;
@property(nonatomic,assign) NSInteger endMonth;
@property(nonatomic,assign) NSInteger endDay;
@property(nonatomic,assign) NSInteger endHour;
//请假结束时间
@property(nonatomic,strong) UILabel *askTimeEndLabel;
@property(nonatomic,strong) NSString *askStartTime;
@property(nonatomic,strong) NSString *askEndTime;
//请假时长
@property(nonatomic,strong) UILabel *askTimeLengthLabel;
//请假事由
@property(nonatomic,strong) UITextView *askResonLabel;
//审批人
@property(nonatomic,strong) UILabel *personLabel;
@property(nonatomic,strong) SINASchoolModel *personModel;
@property(nonatomic,strong) UIView *personUiview;

@property(nonatomic,strong) NSString *selectTimeFlag;

@property(nonatomic,strong) UIButton *commitButtonView;

@property(nonatomic,assign) BOOL isCommit;

//请假类型
@property(nonatomic,strong) NSMutableArray *askLeaveTypeArray;
//审批人
@property(nonatomic,strong) NSMutableArray *peopleTypeArray;

//添加附件
@property(nonatomic,strong) UIView *addImageUiView;
@property(nonatomic,strong) UIView *addImageParentUiView;
@property(nonatomic,strong) UIView *addImageParentParentUiView;
@property(nonatomic,strong) UILabel *otherTitleLabel;

//附件列表
@property(nonatomic,strong) NSMutableArray *otherImageArray;


@property(nonatomic) NSInteger selectImageNumber;
@property(nonatomic) CGRect            preframe;
//选择图片的宽度
@property(nonatomic) NSInteger imageWidth;
//图片之间的列间隔
@property(nonatomic) NSInteger imageRowWidth;
@end

@implementation SINAAskForLeaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.startDay=[SINACalendarUtils getDay];
    self.startMonth =[SINACalendarUtils getMonth];
    self.startYear =[SINACalendarUtils getYear];
    self.startHour =[SINACalendarUtils getHour];
    self.endDay=[SINACalendarUtils getDay];
    self.endMonth =[SINACalendarUtils getMonth];
    self.endYear =[SINACalendarUtils getYear];
    self.endHour =[SINACalendarUtils getHour];
    
    self.imageWidth=76;
    self.imageRowWidth=(mainW-30-4*self.imageWidth)/3;
    self.isCommit = NO;
    
    self.askLeaveTypeArray =[[NSMutableArray alloc]init];
    self.peopleTypeArray =[[NSMutableArray alloc]init];
    self.otherImageArray=[[NSMutableArray alloc]init];
    
    self.preframe=CGRectMake(-15, 15,0,0);
    
    [self configNavigationItem];
    [self initView];
    
    [self addNoticeForKeyboard];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

-(void) initView{
    
    SINAUserModel  *userModel = [[SINACommonUserHelper share] getUserModel];
    
    //请假人
    UIView *titleView  = [[UIView alloc]initWithFrame:CGRectMake(0, 15, mainW, 45)];
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-34, 45)];
    nameLabel.text=userModel.userRealName;
    nameLabel.textAlignment = NSTextAlignmentRight;
    [nameLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    [titleView addSubview:nameLabel];
    
    UILabel *nameTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    nameTitleLabel.text=@"请假人:";
    [nameTitleLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    [titleView addSubview:nameTitleLabel];
    
    [titleView setBackgroundColor:[UIColor whiteColor]];
    [self.mainScrollview addSubview:titleView];
    
    //请假类型
    UIView *typeView  = [[UIView alloc]initWithFrame:CGRectMake(0, 75, mainW, 45)];
    UILabel *typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    typeLabel.text=@"请假类型";
    [typeLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    [typeView addSubview:typeLabel];
    
    UILabel *typeLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(mainW-132, 0, 100, 45)];
    typeLabel2.text=@"请填写";
    self.askLabel = typeLabel2;
    typeLabel2.textAlignment=NSTextAlignmentRight;
    [typeLabel2 setTextColor:[UIColor colorWithHexString:@"#878787"]];
    
    
   
    [[SINAUiviewClickLiserner alloc]viewClickFunction:typeView andWithBlick:^(UIView *view) {
        [self askLeaveSelectTypeFunction];
    }];
    
    UIImageView *typeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(mainW-22, 45/2-13/2, 7, 13)];
    typeImageView.image=[UIImage imageNamed:@"右箭头"];
    [typeView addSubview:typeLabel];
    [typeView addSubview:typeLabel2];
    [typeView addSubview:typeImageView];
    [typeView setBackgroundColor:[UIColor whiteColor]];
    [self.mainScrollview addSubview:typeView];
    
    //请假时间
    UIView *timeView  = [[UIView alloc]initWithFrame:CGRectMake(0, 135, mainW, 150)];
    UILabel *timeStartLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    timeStartLabel.text=@"开始时间:";
    [timeStartLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    
    UILabel *timeInputStartLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainW-172, 0, 140, 45)];
    self.askTimeStartLabel = timeInputStartLabel;
    timeInputStartLabel.text=@"请填写";

    [[SINAUiviewClickLiserner alloc]viewClickFunction:timeInputStartLabel andWithBlick:^(UIView *view) {
        [self selectStartTime];
    }];
    
    timeInputStartLabel.textAlignment=NSTextAlignmentRight;
    [timeInputStartLabel setTextColor:[UIColor colorWithHexString:@"#878787"]];
    UIImageView *timeStartImageView=[[UIImageView alloc]initWithFrame:CGRectMake(mainW-22, 45/2-13/2, 7, 13)];
    timeStartImageView.image=[UIImage imageNamed:@"右箭头"];
    UIView *startDiviView =[[UIView alloc]initWithFrame:CGRectMake(18, 46, mainW-18-15, 1)];
    [startDiviView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
    UILabel *timeEndLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 46, mainW-24, 45)];
    
    timeEndLabel.text=@"结束时间:";
    [[SINAUiviewClickLiserner alloc]viewClickFunction:timeEndLabel andWithBlick:^(UIView *view) {
        [self selectEndTime];
    }];
   
    
    [timeEndLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    UILabel *timeInputEndLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainW-172, 46, 140, 45)];
    timeInputEndLabel.text=@"请填写";
    self.askTimeEndLabel = timeInputEndLabel;
    timeInputEndLabel.textAlignment=NSTextAlignmentRight;
    [timeInputEndLabel setTextColor:[UIColor colorWithHexString:@"#878787"]];
    UIView *endDiviView =[[UIView alloc]initWithFrame:CGRectMake(18, 92, mainW-18-15, 1)];
    [endDiviView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    UIImageView *timeEndImageView=[[UIImageView alloc]initWithFrame:CGRectMake(mainW-22, 46+16, 7, 13)];
    timeEndImageView.image=[UIImage imageNamed:@"右箭头"];
    UILabel *timeLengthLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 100, mainW-24, 45)];
    timeLengthLabel.text=@"请假时长:";
    [timeLengthLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    UILabel *timeInputLengthLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainW-132, 100, 100, 45)];
    timeInputLengthLabel.text=@"";
    self.askTimeLengthLabel =timeInputLengthLabel;
    timeInputLengthLabel.textAlignment=NSTextAlignmentRight;
    [timeInputLengthLabel setTextColor:[UIColor colorWithHexString:@"#878787"]];
    
    
    [timeView addSubview:timeStartLabel];
    [timeView addSubview:timeInputStartLabel];
    [timeView addSubview:timeStartImageView];
    
    [timeView addSubview:startDiviView];
    [timeView addSubview:timeEndLabel];
    [timeView addSubview:timeInputEndLabel];
    [timeView addSubview:endDiviView];
    [timeView addSubview:timeEndImageView];
    [timeView addSubview:timeLengthLabel];
    [timeView addSubview:timeInputLengthLabel];
    [timeView setBackgroundColor:[UIColor whiteColor]];
    [self.mainScrollview addSubview:timeView];
    //请假原因
    UIView *resonView  = [[UIView alloc]initWithFrame:CGRectMake(0, 300, mainW, 90)];
    [resonView setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *resonTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    resonTitleLabel.text=@"请假事由:";
    [resonTitleLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    
    UITextView *resonContentTextView =[[UITextView alloc]initWithFrame:CGRectMake(90, 6, mainW-100, 78)];
    resonContentTextView.text=@"请输入请假事由...";
    self.askResonLabel =resonContentTextView;
    resonContentTextView.delegate=self;
    [resonContentTextView setFont:[UIFont systemFontOfSize:16]];
    [resonContentTextView setTextColor:[UIColor colorWithHexString:@"#878787"]];
    //[resonContentTextView setBackgroundColor:[UIColor colorWithHexString:@"#efefef"]];
    [resonView addSubview:resonTitleLabel];
    [resonView addSubview:resonContentTextView];
    [self.mainScrollview addSubview:resonView];
    //请假附件
    UIView *otherView  = [[UIView alloc]initWithFrame:CGRectMake(0, 405, mainW, self.imageWidth+45+15+15)];
    self.addImageParentParentUiView=otherView;
    [otherView setBackgroundColor:[UIColor whiteColor]];
    UILabel *otherTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    otherTitleLabel.text=@"添加附件:";
    self.otherTitleLabel = otherTitleLabel;
    [otherTitleLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    [otherView addSubview:otherTitleLabel];
    [self.mainScrollview addSubview:otherView];
    
    
    UIView *otherContentTextView =[[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(otherTitleLabel.frame), mainW-30, self.imageWidth+15)];
    self.addImageParentUiView=otherContentTextView;
    [otherContentTextView setBackgroundColor:[UIColor whiteColor]];
   
    UIView *addImageUiView  = [[UIView alloc]initWithFrame:CGRectMake(0, 15, self.imageWidth, self.imageWidth)];
    addImageUiView.backgroundColor=[UIColor whiteColor];
    UIImageView *addImageView= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.imageWidth, self.imageWidth)];
    addImageView.image=[UIImage imageNamed:@"添加照片"];
    
    [addImageUiView addSubview:addImageView];
    
    [otherContentTextView addSubview:addImageUiView];
    
    [otherView addSubview:otherContentTextView];
    self.addImageUiView =addImageUiView;
    
    WeakSelf;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addImageUiView andWithBlick:^(UIView *view) {
        SINAPhotoPopView *popView =[[SINAPhotoPopView alloc]init];
        popView.isShowSaveImage=YES;
        [popView show];
        
        popView.creatPhopeBlock = ^{
            [weakSelf openPhote:1];
            
        };
        popView.selectPhopeBlock = ^{
            [weakSelf openPhote:2];
        };
        
    }];
    
    //审批人
    UIView *personView  = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(otherView.frame)+15, mainW, 45)];
    [personView setBackgroundColor:[UIColor whiteColor]];
    self.personUiview=personView;
    UILabel *personTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, mainW-24, 45)];
    personTitleLabel.text=@"审批人:";
    [personTitleLabel setTextColor:[UIColor colorWithHexString:@"#2e2e2e"]];
    [personView addSubview:personTitleLabel];
    [self.mainScrollview addSubview:personView];
    
    // 1. 创建一个点击事件，点击时触发labelClick方法
    UITapGestureRecognizer *personTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(personSelectFunction)];
    // 2. 将点击事件添加到label上
    [personView addGestureRecognizer:personTapGestureRecognizer];
    // 可以理解为设置label可被点击
    personView.userInteractionEnabled = YES;
    
    UILabel *personInputEndLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainW-172, 0, 140, 45)];
    personInputEndLabel.text=@"请填写";
    self.personLabel = personInputEndLabel;
    personInputEndLabel.textAlignment=NSTextAlignmentRight;
    [personInputEndLabel setTextColor:[UIColor colorWithHexString:@"#878787"]];
    
    [personView addSubview:personInputEndLabel];
    
    
    UIButton *commitButtonView  = [[UIButton alloc]initWithFrame:CGRectMake(12, CGRectGetMaxY(personView.frame)+15, mainW-24, 45)];
    [commitButtonView setBackgroundColor:[UIColor whiteColor]];
    self.commitButtonView = commitButtonView;
    //设置lable的圆角显示
    commitButtonView.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    commitButtonView.layer.borderWidth=0;
    //设置lable的边框颜色
    commitButtonView.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    commitButtonView.clipsToBounds = YES;
    [commitButtonView setBackgroundColor: baseButtonBackGroundColor];
    [commitButtonView setTitle:@"提交" forState: UIControlStateNormal];
    
    
    [self.mainScrollview addSubview:commitButtonView];
    [commitButtonView addTarget:self action:@selector(commitAskForLeaveView) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrollview setBackgroundColor:[UIColor colorWithHexString:@"#f7f7f7"]];
    self.mainScrollview.contentSize=CGSizeMake(mainW, 705);
    
}
-(void)openPhote :(NSInteger) flag{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //可编辑
    picker.allowsEditing = NO;
    //判断是否可以打开照相机
    if (flag==1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //摄像头
            //UIImagePickerControllerSourceTypeSavedPhotosAlbum:相机胶卷
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        } else { //否则打开照片库
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
    }else{
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:picker animated:YES completion:nil];
}
-(void)configNavigationItem{
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title=@"请假申请";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIBarButtonItem *askListButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:@selector(askListClickFunction)];
    [askListButton setImage:[UIImage imageNamed:@"ask_list"]];
    self.navigationItem.rightBarButtonItem = askListButton;
    
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请输入请假事由...";
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请输入请假事由..."]){
        textView.text=@"";
        //[YPXToastView showToastViewWithText:@"最多可输入30字" andDuration:1 andParentView:self.view];
    }
}


-(void) commitAskForLeaveView{
    
    
    //请假类型
    NSString *askType =self.askLabel.text;
    if (askType.length<1||[askType isEqualToString:@"请填写"]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入请假类型" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    //请假时长
    NSString *askTimeLength = self.askTimeLengthLabel.text;
    if (askTimeLength.length<1) {
        NSString *msg = @"";
        NSString *startTime =self.askTimeStartLabel.text;
         NSString *endTime =self.askTimeEndLabel.text;
        if (startTime==nil||[startTime isEqualToString:@"请填写"]) {
            msg = @"请选择开始时间";
        } else if (endTime==nil||[endTime isEqualToString:@"请填写"]) {
            msg = @"请选择结束时间";
        }else{
            msg = @"请假时长最短为2小时";
        }
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:msg sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    //请假事由
    NSString *reson =self.askResonLabel.text;
    
    if (reson.length<1||[reson isEqualToString:@"请输入请假事由..."]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入请假事由..." sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    
    
    
    //审批人
    if (self.personModel==nil) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择审批人" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    if (self.isCommit==YES) {
        return;
    }
    self.isCommit=YES;
    [self.otherImageArray removeAllObjects];
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    
    for (NSInteger i=0; i<self.addImageParentUiView.subviews.count; i++) {
        if (i!=0) {
            UIView *itemView = self.addImageParentUiView.subviews[i];
            UIImageView *flagImageView = itemView.subviews[0];
            UIImage *imge =flagImageView.image;
            [imageArray addObject:imge];
        }
    }
    if (imageArray.count>0) {
        
        [self startLoading];
        WeakSelf;
        //上传图片
        [CXAFNHttpRequestUtils httpPostFileRequestWithParamters:nil withImageArray:imageArray success:^(id responseData) {
            NSString *code = responseData[@"code"];
            [weakSelf stopLoading];
            if ([code isEqualToString:@"1000"]) {
                NSArray *imageArray = responseData[@"content"];
                if (imageArray!=nil&&imageArray.count>0) {
                    for (NSInteger i=0; i<imageArray.count; i++) {
                        NSString *titleImageUrl = imageArray[i];
                        [self.otherImageArray addObject:titleImageUrl];
                    }
                    [weakSelf commidAskLeaveFucntion];
                }else{
                    WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"上传图片数据异常" sureBtn:@"确定"
                                                                         cancleBtn:nil];
                    [alertView showXLAlertView];
                    
                    self.isCommit=NO;
                }
                
                
                
                
            }else if ([code isEqualToString:@"1003"]) {
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                self.isCommit=NO;
            }else{
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                self.isCommit=NO;
            }
            
        } failure:^(NSError *error, NSInteger code) {
            DLog(@"加载 失败  %@ ",error.debugDescription);
            [weakSelf stopLoading];
            self.isCommit=NO;
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }];
        
        return;
    }else{

        [self startLoading];
         [self commidAskLeaveFucntion];
    }
}

//提交请假数据 
-(void) commidAskLeaveFucntion{
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
   
    //请假人id String leaveid
    [dict setObject: [[SINACommonUserHelper share] getUserModel].userUid forKey:@"leaveid"];
    [dict setObject: [[SINACommonUserHelper share] getUserModel].userRealName forKey:@"realname"];
    //请假类型 String typeid
    [dict setObject: [NSString stringWithFormat:@"%@",self.askTypeModel.schoolId] forKey:@"typeid"];
    //请假开始时间 String starttime
    if (self.askStartTime.length==0) {
        NSDateFormatter * df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"yyyy-MM-dd HH时";
        NSString * dateString2 = self.askTimeStartLabel.text;
        NSDate * date2 = [df dateFromString:dateString2];
        self.askStartTime=[NSString stringWithFormat:@"%llu",[SINACalendarUtils getDateTimeTOMilliSeconds:date2]];
    }
    [dict setObject:[NSString stringWithFormat:@"%@",self.askStartTime ] forKey:@"starttime"];
    //请假结束时间 String endtime
    if (self.askEndTime.length==0) {
        NSDateFormatter * df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"yyyy-MM-dd HH时";
        NSString * dateString2 = self.askTimeEndLabel.text;
        NSDate * date2 = [df dateFromString:dateString2];
        self.askEndTime=[NSString stringWithFormat:@"%llu",[SINACalendarUtils getDateTimeTOMilliSeconds:date2]];
    }
    [dict setObject: [NSString stringWithFormat:@"%@",self.askEndTime] forKey:@"endtime"];
    //请假原因 String reason
    [dict setObject: self.askResonLabel.text forKey:@"reason"];
    //审批人id String approvalid
    [dict setObject:[NSString stringWithFormat:@"%@", self.personModel.schoolId] forKey:@"approvalid"];
    //附件图片 String imgs
    NSString *imgs=@"";
    if (self.otherImageArray.count>0) {
        for (NSInteger i=0; i<self.otherImageArray.count; i++) {
            if (imgs.length==0) {
                imgs = self.otherImageArray[i];
            } else {
            imgs=[NSString stringWithFormat:@"%@,%@",imgs,self.otherImageArray[i]];
            }
        }
    }
     [dict setObject:[NSString stringWithFormat:@"%@", imgs] forKey:@"imgs"];

    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userCommitAskLeaveUrl] withParamters:dict2 success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假提交成功" sureBtn:@"去查看"
                                                                 cancleBtn:nil];
            alertView.resultIndex = ^(NSInteger index) {
                [weakSelf askListClickFunction];
                 [weakSelf clearDataFunction];
            };
            [alertView showXLAlertView];
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        self.isCommit=NO;
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        self.isCommit=NO;
    }];
}


//清空数据
-(void)clearDataFunction{
    [self.otherImageArray removeAllObjects];
    self.askTimeEndLabel.text=@"请填写";
    self.askTimeStartLabel.text=@"请填写";
    self.askStartTime=@"";
    self.askEndTime=@"";
    self.personLabel.text=@"请填写";
    self.askLabel.text=@"请填写";
    self.askResonLabel.text=@"请输入请假事由";
    self.askTimeLengthLabel.text=@"";
    [self.addImageParentUiView removeAllSubviews];
    [self.addImageParentUiView addSubview:self.addImageUiView];
    
    
    self.addImageParentParentUiView.frame=CGRectMake(0, 405, mainW, self.imageWidth+45+15+15);
    self.addImageParentUiView.frame=CGRectMake(15, 45, mainW-30, self.imageWidth+15);
    self.addImageUiView.frame=CGRectMake(0, 15,self.imageWidth, self.imageWidth);
    self.personUiview.frame=CGRectMake(0, CGRectGetMaxY(self.addImageParentParentUiView.frame)+15, CGRectGetWidth(self.personUiview.frame), CGRectGetHeight(self.personUiview.frame));
    self.commitButtonView.frame=CGRectMake(self.commitButtonView.frame.origin.x, CGRectGetMaxY(self.personUiview.frame)+15, CGRectGetWidth(self.commitButtonView.frame), CGRectGetHeight(self.commitButtonView.frame));
    self.mainScrollview.contentSize=CGSizeMake(mainW, CGRectGetMaxY(self.commitButtonView.frame)+20);
    
    [self.otherImageArray removeAllObjects];
}

//选择类型
-(void)selectTypeClick{
    SINAStudentAskLeaveTypeSelectView *alertview = [[SINAStudentAskLeaveTypeSelectView alloc]init];
    [alertview show];
}

//选择开始时间
-(void) selectStartTime{
    self.selectTimeFlag = @"START";
    //选择开始时间
    [self selectTime:self.startYear :self.startMonth :self.startDay :self.startHour];
    
    
    
}
//选择结束时间
-(void) selectEndTime{
    self.selectTimeFlag = @"END";
    //选择结束时间
    [self selectTime:self.endYear :self.endMonth :self.endDay :self.endHour];
    
}
//选择时间
-(void) selectTime:(NSInteger)year :(NSInteger) month :(NSInteger) day  :(NSInteger) hour{

    SINACommonTimePickView *timePickView =[[SINACommonTimePickView alloc]init];
    if ([self.selectTimeFlag isEqualToString:@"END"]) {
        timePickView.titleLabel.text=@"结束时间";
    } else {
        timePickView.titleLabel.text=@"开始时间";
    }
 [timePickView comTimePicViewDefaultSelect:timePickView andWithYear:year andWithMonth:month andWithDay:day andWithHoure:hour andWithMin:0 andWithSec:0];
    
    timePickView.delegate=self;
    timePickView.currentTimeStyle=YEAER_MAONTH_DAY_HOURE;
    //    [timePickView comTimePicViewDefaultSelect:timePickView andWithYear:2016 andWithMonth:1 andWithDay:22 andWithHoure:23 andWithMin:23 andWithSec:23];
    [timePickView show];
}

-(void)commonTimeSelectPickView:(SINACommonTimePickView *)customPickView andWithYear:(NSString *)year andWithMonth:(NSString *)month andWithDay:(NSString *)day andWithHoure:(NSString *)hour andWithMin:(NSString *)min andWithSec:(NSString *)sec{
    
    NSString * monthString=[NSString stringWithFormat:@"%ld",[month integerValue] ];
    if ([month integerValue]<10) {
        monthString=[NSString stringWithFormat:@"0%@",monthString];
    }
    
    NSString *dayString=[NSString stringWithFormat:@"%ld",[day integerValue] ];;
    if ([day integerValue]<10) {
        dayString=[NSString stringWithFormat:@"0%@",dayString];
    }
    
    NSString *hourString=[NSString stringWithFormat:@"%ld",[hour integerValue] ];
    if ([hour integerValue]<10) {
        //hourString=[NSString stringWithFormat:@"0%@",hour];
    }
    
    if ([self.selectTimeFlag isEqualToString:@"END"]) {
        self.endYear = [year integerValue];
        self.endMonth = [month integerValue];;
        self.endDay = [day integerValue];;
        self.endHour = [hour integerValue];;
        
        self.askTimeEndLabel.text=[NSString stringWithFormat:@"%@-%@-%@ %@时",year,monthString,dayString,hourString];
    } else {
        self.startYear=[year integerValue];;
        self.startMonth=[month integerValue];;
        self.startDay=[day integerValue];;
        self.startHour=[hour integerValue];
        
        self.askTimeStartLabel.text=[NSString stringWithFormat:@"%@-%@-%@ %@时",year,monthString,dayString,hourString];
    }
    
    
     if ([self.selectTimeFlag isEqualToString:@"END"]) {
         NSString *startTimeString = self.askTimeStartLabel.text;
         if (startTimeString.length>1&&![startTimeString isEqualToString:@"请填写"]) {
             NSDateFormatter * df = [[NSDateFormatter alloc] init];
             df.dateFormat = @"yyyy-MM-dd HH时";
             
             NSString * dateString1 = startTimeString;
             NSString * dateString2 = self.askTimeEndLabel.text;
             
             NSDate * date1 = [df dateFromString:dateString1];
             NSDate * date2 = [df dateFromString:dateString2];
             
             self.askEndTime=[NSString stringWithFormat:@"%llu",[SINACalendarUtils getDateTimeTOMilliSeconds:date2]];
             
             
             NSTimeInterval time = [date2 timeIntervalSinceDate:date1]; //date1是前一个时间(早)，date2是后一个时间(晚)
             
             DLog(@"time = %f", time); // 结果就是两个时间点之间相差的秒数，如果要精确时间，修改NSDateFormatter的形式，并把两个时间对应调整就行
             
             if (time<0) {
                 
                 WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择正确的开始时间" sureBtn:@"确定"
                                                                      cancleBtn:nil];
                 [alertView showXLAlertView];
                 self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                 self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                 self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
             }else if(time==0){
                 WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最少为2小时" sureBtn:@"确定"
                                                                      cancleBtn:nil];
                 [alertView showXLAlertView];
                 self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                 self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                 self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
             }else{
                 NSTimeInterval terval = time/60/60/24;
                 NSInteger hour =0;
                 if (terval<1) {
                     hour=time/3600;
                 } else {
                     hour = ((terval - ((int)(terval)))*24);
                 }
                 
                 if ((int)terval>60) {
                     WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最长为60天" sureBtn:@"确定"
                                                                          cancleBtn:nil];
                     [alertView showXLAlertView];
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                     self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                     self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
                     return;
                 }
                 
                 if (((int)terval)<=0&&hour<2) {
                     
                     WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最少为2小时" sureBtn:@"确定"
                                                                          cancleBtn:nil];
                     [alertView showXLAlertView];
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                     self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                     self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
                     return;
                 }
                 
                 if (((int)terval)<=0) {
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%ld小时",hour];
                 } else {
                     if (((int)hour)<=0) {
                         self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%d天",(int)terval];
                     } else {
                         self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%d天%ld小时",(int)terval,hour];
                     }
                 }
             }
         }
         
     }else{
         NSString *endTimeString = self.askTimeEndLabel.text;
         if (endTimeString.length>1&&![endTimeString isEqualToString:@"请填写"]) {
             NSDateFormatter * df = [[NSDateFormatter alloc] init];
             df.dateFormat = @"yyyy-MM-dd HH时";
             
             NSString * dateString1 = self.askTimeStartLabel.text;
             NSString * dateString2 = endTimeString;
             
             NSDate * date1 = [df dateFromString:dateString1];
             NSDate * date2 = [df dateFromString:dateString2];
             
             self.askStartTime=[NSString stringWithFormat:@"%llu",[SINACalendarUtils getDateTimeTOMilliSeconds:date1]];
             
             
             NSTimeInterval time = [date2 timeIntervalSinceDate:date1]; //date1是前一个时间(早)，date2是后一个时间(晚)
             
             DLog(@"time = %f", time); // 结果就是两个时间点之间相差的秒数，如果要精确时间，修改NSDateFormatter的形式，并把两个时间对应调整就行
             if (time<0) {
                 
                 WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择正确的开始时间" sureBtn:@"确定"
                                                                      cancleBtn:nil];
                 [alertView showXLAlertView];
                 self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                 self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                 self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
             }else if(time==0){
                 WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最少为2小时" sureBtn:@"确定"
                                                                      cancleBtn:nil];
                 [alertView showXLAlertView];
                 self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                 self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                 self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
             }else{
                 NSTimeInterval terval = time/60/60/24;
                 NSInteger hour =0;
                 if (terval<1) {
                     hour=time/3600;
                 } else {
                     hour = ((terval - ((int)(terval)))*24);
                 }
                 
                 if ((int)terval>60) {
                     WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最长为60天" sureBtn:@"确定"
                                                                          cancleBtn:nil];
                      [alertView showXLAlertView];
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                     self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                     self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
                     return;
                 }
                 
                 if (((int)terval)<=0&&hour<2) {
                     
                     WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请假时长最少为2小时" sureBtn:@"确定"
                                                                          cancleBtn:nil];
                     [alertView showXLAlertView];
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@""];
                     self.askTimeStartLabel.text=[NSString stringWithFormat:@"请填写"];
                     self.askTimeEndLabel.text=[NSString stringWithFormat:@"请填写"];
                     return;
                 }
                 
                 if (((int)terval)<=0) {
                     self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%ld小时",hour];
                 } else {
                     if (((int)hour)<=0) {
                         self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%d天",(int)terval];
                     } else {
                         self.askTimeLengthLabel.text=[NSString stringWithFormat:@"%d天%ld小时",(int)terval,hour];
                     }
                     
                 }
                 
             }
             
         }
     }
    
    
}
- (void)dismissServiceTimeSelectPickView:(SINACommonTimePickView *)customPickView
{
    
}

#pragma mark -  BasePickViewDelegate method
- (void)dismissBasePickView:(SINABasePickView *)customPickView
{
    
}

-(void)pickerArea:(SINAPickCityView *)pickerArea selectProvince:(NSString *)province selectCity:(NSString *)city{
    
}

#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
//    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
//    CGFloat offset = (self.askResonLabel.frame.origin.y+self.askResonLabel.frame.size.height+10) - (self.view.frame.size.height - kbHeight);
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
    
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f,-20, self.view.frame.size.width, self.view.frame.size.height);
        }];
    
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

//审批人
-(void) personSelectFunction{
    if (self.peopleTypeArray.count==0) {
        [self loadingPeopleFunction];
    }else{
        [self askLeaveSelectTypeFunction:self.peopleTypeArray andWithTitle:@"审批人" andTag:102];
    }
}
//请假类型
-(void) askLeaveSelectTypeFunction{
    
    if (self.askLeaveTypeArray.count==0) {
        [self loadingAskForLeaveTypeFunction];
    }else{
        [self askLeaveSelectTypeFunction:self.askLeaveTypeArray andWithTitle:@"请假类型" andTag:101];
    }
    
    
}
-(void) askLeaveSelectTypeFunction:(NSMutableArray*) arrayData andWithTitle:(NSString *)title andTag:(NSInteger)tag{
    
    
    SINACustomModelPickView *pickerSingle = [[SINACustomModelPickView alloc]init];
    
    [pickerSingle setDataArray:arrayData];
    [pickerSingle setDefalutSelectRowStr:0];
    pickerSingle.titleLabel.text=title;
    pickerSingle.tag=tag;
    
    [pickerSingle setDelegate:self];
    
    [pickerSingle show];
    [self.view endEditing:YES];
}

-(void)customPickView:(SINACustomPickView *)customPickView selectedTitle:(NSString *)selectedTitle{
    
    if (customPickView.tag==101) {
        
        self.askLabel.text=selectedTitle;
    } else {
        self.personLabel.text=selectedTitle;
    }
    
}

-(void)askListClickFunction{
    SINAAskListPageViewController *listViewController =[[SINAAskListPageViewController alloc]initWithNibName:@"SINAAskListPageViewController" bundle:nil];
    [self.navigationController pushViewController:listViewController animated:YES];
}


//加载请假类型
-(void) loadingAskForLeaveTypeFunction{

    [self startLoading];
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?pid=1",baseUrl,typeByPidUrl] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [weakSelf.askLeaveTypeArray removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINASchoolModel *model = [[SINASchoolModel alloc]init];
                    model.schoolName=dic[@"name"];
                    model.schoolId=dic[@"id"];
                    SINACommonPopModel * popModel =[[SINACommonPopModel alloc]init];
                    popModel.title = model.schoolName;
                    popModel.model = model;
                    [weakSelf.askLeaveTypeArray addObject:popModel];
                }
                
                [self askLeaveSelectTypeFunction:self.askLeaveTypeArray andWithTitle:@"请假类型" andTag:101];
                
            }else{
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"暂无数据" sureBtn:@"确定"
                                                                     cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
           
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
    
}
//加载审批人
-(void) loadingPeopleFunction{

    [self startLoading];
    SINAUserModel *userModel=[[SINACommonUserHelper share]getUserModel];
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?uuid=%@",baseUrl,approverPeopleUrl,userModel.userUid] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [weakSelf.peopleTypeArray removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINASchoolModel *model = [[SINASchoolModel alloc]init];
                    model.schoolName=dic[@"realname"];
                    model.schoolId=dic[@"uuid"];
                    SINACommonPopModel * popModel =[[SINACommonPopModel alloc]init];
                    popModel.title = model.schoolName;
                    popModel.model = model;
                    [weakSelf.peopleTypeArray addObject:popModel];
                }
                
                [self askLeaveSelectTypeFunction:self.peopleTypeArray andWithTitle:@"审批人" andTag:102];
                
            }else{
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"暂无数据" sureBtn:@"确定"
                                                                     cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
            
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
    
}
-(void)customModelPickView:(SINACustomModelPickView *)customPickView selectedTitle:(SINACommonPopModel *) selectedModel{
    if (customPickView.tag==101) {
        self.askLabel.text=selectedModel.title;
        self.askTypeModel=(SINASchoolModel*)selectedModel.model;
    }else if (customPickView.tag==102) {
        self.personLabel.text=selectedModel.title;
        self.personModel=(SINASchoolModel*)selectedModel.model;
    }
}

//拍摄完成后要执行的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    __weak typeof(self) weakSelf = self;
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType; //指定用户选择的媒体类型
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage; // 原始图片
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage; // 修改后的图片
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect; // 裁剪尺寸
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL; // 媒体的URL
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL NS_AVAILABLE_IOS(4_1); // 原件的URL
    //    UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata //当数据来源是相机的时候获取到的静态图像元数据，可以使用phtoho框架进行处理
    //
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    NSData *data;
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        //切忌不可直接使用originImage，因为这是没有经过格式化的图片数据，可能会导致选择的图片颠倒或是失真等现象的发生，从UIImagePickerControllerOriginalImage中的Origin可以看出，很原始
        UIImage *originImage = [info objectForKey:UIImagePickerControllerEditedImage];
        if (originImage==nil) {
            originImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        
        //图片压缩，因为原图都是很大的，不必要传原图
        UIImage *scaleImage = [self scaleImage:originImage toScale:0.6];
        
        //以下这两步都是比较耗时的操作，最好开一个HUD提示用户，这样体验会好些，不至于阻塞界面
        if (UIImagePNGRepresentation(scaleImage) == nil) {
            //将图片转换为JPG格式的二进制数据
            data = UIImageJPEGRepresentation(scaleImage, 1);
        } else {
            //将图片转换为PNG格式的二进制数据
            data = UIImagePNGRepresentation(scaleImage);
        }
        
        //将二进制数据生成UIImage
        UIImage *image = [UIImage imageWithData:data];
        
        UIView *itemAddImageView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.preframe)+15, self.preframe.origin.y, self.imageWidth, self.imageWidth+15)];
        
        UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.imageWidth, self.imageWidth)];
        imageView.image = image;
        [itemAddImageView addSubview:imageView];
        
        UIView *deleteView =[[UIView alloc]initWithFrame:CGRectMake(self.imageWidth-40, -10, 50, 50)];
        UIImageView *deleteImageView =[[UIImageView alloc]initWithFrame:CGRectMake(30, 0, 20, 20)];
        deleteImageView.image = [UIImage imageNamed:@"删除"];
        [deleteView addSubview:deleteImageView];
        [itemAddImageView addSubview:deleteView];
        self.selectImageNumber++;
        
        itemAddImageView.tag =100+self.selectImageNumber;
        
        [self.addImageParentUiView addSubview:itemAddImageView];
        
        
        //删除功能 start
        
        [[SINAUiviewClickLiserner alloc]viewClickFunction:deleteView andWithBlick:^(UIView *view) {
            NSInteger deleteFlag =itemAddImageView.tag-100;
            DLog(@"删除 %ld",deleteFlag);
            //删除
            for (UIView *subviews in [weakSelf.addImageParentUiView subviews]) {
                if (subviews.tag==itemAddImageView.tag) {
                    [subviews removeFromSuperview];
                }
            }
            //重新排列所有的子view
            for (NSInteger i=0; i<[[weakSelf.addImageParentUiView subviews] count]; i++) {
                UIView *subviews =[weakSelf.addImageParentUiView subviews][i];
                [UIView animateWithDuration:0.35 animations:^{
                    if (i>0&&i<=4) {
                        subviews.frame=CGRectMake((weakSelf.imageWidth+15)*(i-1), 15, weakSelf.imageWidth, weakSelf.imageWidth);
                    }else if(i>4){
                        subviews.frame=CGRectMake((weakSelf.imageWidth+15)*(i-5), weakSelf.imageWidth+30, weakSelf.imageWidth, self.imageWidth);
                    }
                }];
            }
            if ([[weakSelf.addImageParentUiView subviews]count]>1&&[[weakSelf.addImageParentUiView subviews]count]<5) {
                
                UIView *lastSubviews=[weakSelf.addImageParentUiView subviews][[[weakSelf.addImageParentUiView subviews] count]-1 ];
                weakSelf.preframe=lastSubviews.frame;
                [UIView animateWithDuration:0.35 animations:^{
                    weakSelf.addImageUiView.frame=CGRectMake(lastSubviews.frame.origin.x+15+weakSelf.imageWidth, lastSubviews.frame.origin.y, weakSelf.imageWidth, weakSelf.imageWidth);
                }];
                
            }else if([[weakSelf.addImageParentUiView subviews]count]==5){
                weakSelf.preframe=CGRectMake(-15, 15+15+self.imageWidth,0,0);
                [UIView animateWithDuration:0.35 animations:^{
                    weakSelf.addImageUiView.frame=CGRectMake(0, 15+15+self.imageWidth, weakSelf.imageWidth, weakSelf.imageWidth);
                }];
            }else if([[weakSelf.addImageParentUiView subviews]count]>5){
                UIView *lastSubviews=[weakSelf.addImageParentUiView subviews][[[weakSelf.addImageParentUiView subviews] count]-1 ];
                weakSelf.preframe=lastSubviews.frame;
                [UIView animateWithDuration:0.35 animations:^{
                    weakSelf.addImageUiView.frame=CGRectMake(lastSubviews.frame.origin.x+15+weakSelf.imageWidth, lastSubviews.frame.origin.y, weakSelf.imageWidth, weakSelf.imageWidth);
                }];
            }else{
                weakSelf.preframe=CGRectMake(-15, 15,0,0);
                [UIView animateWithDuration:0.35 animations:^{
                    weakSelf.addImageUiView.frame=CGRectMake(0, 15,weakSelf.imageWidth,weakSelf.imageWidth);
                }];
            }
            
            if ([[weakSelf.addImageParentUiView subviews]count]<=4) {
                weakSelf.addImageParentUiView.frame=CGRectMake(15, CGRectGetMaxY(weakSelf.otherTitleLabel.frame), mainW-30, weakSelf.imageWidth+15);
                weakSelf.addImageParentParentUiView.frame=CGRectMake(0, weakSelf.addImageParentParentUiView.frame.origin.y, mainW, CGRectGetMaxY(weakSelf.addImageParentUiView.frame)+15);
            } else {
                weakSelf.addImageParentUiView.frame=CGRectMake(15, CGRectGetMaxY(weakSelf.otherTitleLabel.frame), mainW-30,+ weakSelf.imageWidth+15+15+weakSelf.imageWidth+15);
                weakSelf.addImageParentParentUiView.frame=CGRectMake(0, weakSelf.addImageParentParentUiView.frame.origin.y, mainW, CGRectGetMaxY(weakSelf.addImageParentUiView.frame)+15);
            }
            weakSelf.addImageUiView.hidden=NO;
            
            
            self.personUiview.frame=CGRectMake(0, CGRectGetMaxY(self.addImageParentParentUiView.frame)+15, CGRectGetWidth(self.personUiview.frame), CGRectGetHeight(self.personUiview.frame));
            self.commitButtonView.frame=CGRectMake(self.commitButtonView.frame.origin.x, CGRectGetMaxY(self.personUiview.frame)+15, CGRectGetWidth(self.commitButtonView.frame), CGRectGetHeight(self.commitButtonView.frame));
            self.mainScrollview.contentSize=CGSizeMake(mainW, CGRectGetMaxY(self.commitButtonView.frame)+20);
        }];
        
        //删除功能 end
        
        
        self.preframe=CGRectMake(CGRectGetMaxX(self.preframe)+self.imageRowWidth, self.preframe.origin.y, self.imageWidth, self.imageWidth);
        self.addImageUiView.frame=CGRectMake(self.preframe.origin.x+self.imageRowWidth+self.imageWidth, self.preframe.origin.y, self.imageWidth, self.imageWidth);
        
        
        if (CGRectGetMaxX(self.addImageUiView.frame)+15>=mainW&&self.addImageParentUiView.subviews.count<7) {
            self.preframe=CGRectMake(-15, self.imageWidth+30, 0,0);
            self.addImageUiView.frame=CGRectMake(0, self.imageWidth+30, self.imageWidth, self.imageWidth);
            self.addImageParentUiView.frame=CGRectMake(self.addImageParentUiView.frame.origin.x, self.addImageParentUiView.frame.origin.y, self.addImageParentUiView.frame.size.width, self.addImageParentUiView.frame.size.height+self.imageWidth+15);
            self.addImageParentParentUiView.frame=CGRectMake(self.addImageParentParentUiView.frame.origin.x, self.addImageParentParentUiView.frame.origin.y, self.addImageParentParentUiView.frame.size.width, self.addImageParentParentUiView.frame.size.height+self.imageWidth+15);
        }
        
        if (self.addImageParentUiView.subviews.count>=9) {
            self.addImageUiView.hidden=YES;
            self.preframe=CGRectMake(-15, 15,0,0);
        }else{
            self.addImageUiView.hidden=NO;
        }
        
        self.personUiview.frame=CGRectMake(0, CGRectGetMaxY(self.addImageParentParentUiView.frame)+15, CGRectGetWidth(self.personUiview.frame), CGRectGetHeight(self.personUiview.frame));
        self.commitButtonView.frame=CGRectMake(self.commitButtonView.frame.origin.x, CGRectGetMaxY(self.personUiview.frame)+15, CGRectGetWidth(self.commitButtonView.frame), CGRectGetHeight(self.commitButtonView.frame));
        self.mainScrollview.contentSize=CGSizeMake(mainW, CGRectGetMaxY(self.commitButtonView.frame)+20);
        
        [self.otherImageArray removeAllObjects];
    }
    
}


#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
-(void)dismissCustomPickView:(SINACustomModelPickView *)customPickView{
    
}
#pragma mark - text view delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text length] > 30) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 30)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
